//
//  BLinNetTool.swift
//  Emergency_Swift
//
//  Created by 王宗成 on 2019/4/24.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit
import Alamofire

class BLinNetTool: NSObject {

    //这个是逃逸闭包 代码块执行完了才会执行
    class func postRequestData(path:String,params:[String:Any]? = ["uid":""],finishCallBack:@escaping (_ responseCall:Any?,_ success:Bool,_ msg:String,_ code:Int)->())->Void {
  
        let temPath = "www.baidu.com"
        
        let headers: HTTPHeaders = [
            "Accept-Encoding" : "br, gzip, deflate",
            "Content-Type" : "application/x-www-form-urlencoded",
        ]
        
        //第三方网络请求 不要设置编码格式，帮邻后台不支持编码后的格式
        Alamofire.request(temPath, method: .post, parameters: params, headers: headers).responseJSON { (response) in
            
            if params != nil{
                
                print(response.request?.url ?? "url")
                print(params ?? String())
            }
          
            guard let result = response.result.value else {
                
                print(response.result.error ?? "请求出错")
                
                return
            }
            
            print("返回数据")
            debugPrint(result)
            
            //返回的不是key value形式 则直接返回
            guard let dict = result as? [String:Any] else
            {
                return;
            }
        
            var success = false
            
            guard let code = dict["code"] as? Int else
            {
                return;
            }
            
            var resultMsg:String = dict["msg"] as! String
            
            if code == 1011{
                
                resultMsg = "你的账号在其它地方登录，请重新登录"
               
            }
            
            if code==0 {
                
                success = true
            }
  
            finishCallBack(dict["data"],success,resultMsg,code)
        
        }
        
    }
    
}
